package Hot100;

import Tree.TreeNode;

public class 二叉搜索树中第K小的元素 {

    private int count;
    private int res;

    public int kthSmallest(TreeNode root, int k) {
        count = k;
        inorder(root);
        return res;
    }

    private void inorder(TreeNode root) {
        if (root == null) return;
        inorder(root.left);
        if (count == 0) return;
        if (--count == 0) {
            res = root.val;
            return;
        }
        inorder(root.right);
    }
}
